//
//  MySetViewController.m
//  used
//
//  Created by lanou on 16/7/20.
//  Copyright © 2016年 王凯. All rights reserved.
//

#import "MySetViewController.h"

@interface MySetViewController ()

/** 数据 */
@property (nonatomic, strong) NSArray *settings;

@property (nonatomic, strong) NSArray *setting;

/** 退出 */
@property (nonatomic, strong) UIButton *loginout;

@end

@implementation MySetViewController

- (UIButton *)loginout {
    if (!_loginout) {
        _loginout = [[UIButton alloc] initWithFrame:CGRectMake(30, 264, kScreenW - 60, 35)];
        [_loginout setTitle:@"退出登录" forState:UIControlStateNormal];
        [_loginout setBackgroundColor:KLRGBColor(245, 213, 29)];
        [_loginout addTarget:self action:@selector(loginoutAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginout;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view addSubview:self.loginout];
    [self setTableView];
    
}

- (void)setTableView
{
    
    self.tableView.backgroundColor = KLRGBColor(240, 240, 240);
    
    self.settings = @[@"消息免打扰"];
    self.setting = @[@"关于Buy兔",@"意见反馈", @"清理缓存"];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
   
}

- (void)loginoutAction {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定退出" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [BmobUser logout];
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:nil];
    [alert addAction:confirm];
    [alert addAction:cancel];
    
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark - <UITableViewDelegate>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 1;
    }else {
        return 3;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.textLabel.text = self.settings[indexPath.row];
        cell.textLabel.textColor = [UIColor darkGrayColor];
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
     
        UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
        cell.accessoryView = switchView;
        switchView.on = NO;
        switchView.onTintColor = [UIColor yellowColor];
        [switchView addTarget:self action:@selector(modelSwitchView:) forControlEvents:UIControlEventValueChanged];
        
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        //分割线
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 50, self.view.width, 1)];
        line.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.4];
        [cell addSubview:line];
        
        return cell;
        
    }else {

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (indexPath.row == 2) {
            
            
            
            cell.textLabel.text = [NSString stringWithFormat:@"清除缓存(%.1fM)", [self caches]];
        }else {
            
            cell.textLabel.text = self.setting[indexPath.row];
        }
        cell.textLabel.textColor = [UIColor darkGrayColor];
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        //分割线
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 50, self.view.width, 1)];
        line.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.4];
        [cell addSubview:line];
        
        return cell;
    }
    
}



#pragma mark - <UITableViewDelegate>
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 30)];
    view.backgroundColor = self.view.backgroundColor;
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

        return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.section == 0 ) {
    
        //Wi-Fi
    }else if (indexPath.section == 1 && indexPath.row == 0) {
        
        //关于XX
        
    }else if (indexPath.section == 1 && indexPath.row == 1) {
    
        //意见反馈
    
    }else if (indexPath.section == 1 && indexPath.row == 2) {

        //清理缓存
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确定要清空缓存?" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
           
            NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            NSString *cachePath = [caches stringByAppendingPathComponent:@"default/com.hackemist.SDWebImageCache.default"];
             [[NSFileManager defaultManager] removeItemAtPath:cachePath error: nil];
            [self.tableView reloadData];
        }];
        
        [alert addAction:cancel];
        [alert addAction:sure];
        
        [self presentViewController:alert animated:YES completion:nil];
        
    
    }
    
}

#pragma mark - Switch 点击事件

- (void)modelSwitchView:(UISwitch *)sender
{

    NSLog(@"model");
}




- (double)caches{
    NSFileManager *manager = [NSFileManager defaultManager];
    
    NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *cachePath = [caches stringByAppendingPathComponent:@"default/com.hackemist.SDWebImageCache.default"];
    
    NSDirectoryEnumerator *fileEnumerator = [manager enumeratorAtPath:cachePath];
    NSInteger totalSize = 0;
    for (NSString *fileName in fileEnumerator) {
        NSString *filepath = [cachePath stringByAppendingPathComponent:fileName];
        
        NSDictionary *attrs = [manager attributesOfItemAtPath:filepath error:nil];
        if ([attrs[NSFileType] isEqualToString:NSFileTypeDirectory]) continue;
        
        totalSize += [attrs[NSFileSize] integerValue];
    }
    

    return totalSize/1000/1000.0;
}

@end
